package netWorkPrograming.test.test02;

import sun.net.www.content.image.png;

import java.awt.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/5/28
 */
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(8888);
        System.out.println("服务端监听端口号：8888");
        Socket socket = ss.accept();
        System.out.println("成功接入客户端！");

        // 先准备输出目录和文件
        File dir = new File("C:/Users/23838/Desktop/temp");
        if (!dir.exists()) dir.mkdirs();
        File outFile = new File(dir, "upload_" + System.currentTimeMillis() + ".png");

        try (
                BufferedInputStream  bis = new BufferedInputStream(socket.getInputStream());
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile));
        ) {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            bos.flush();
            System.out.println("写入成功，保存在：" + outFile.getAbsolutePath());
        } finally {
            socket.close();
            ss.close();
        }
    }
}
